﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public Label[,] MasLbl = new Label[9, 9];
       
        NumberChoice number = new NumberChoice();

        Sudoku sudMas;

        int amount=0;

        public MainForm()    
        {
            InitializeComponent();
            Draw();
            sudMas = new Sudoku();
            sudMas.ReadInformation(Level.filename);
            Obn();
        }

        private void Obn()
        {
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (sudMas.Mas[i, j] != 0)
                    {
                        MasLbl[i, j].Text = sudMas.Mas[i, j].ToString();
                        amount++;
                    }
                }
            }
        }

        private void Mas_Click(object sender, EventArgs e)
        {
            int x = 0;
            int y = 0;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (MasLbl[i, j] == sender)
                    {
                        y = i;
                        x = j;
                    }
                }
            }
            if (sudMas.Masb[y, x] == true)
            {
                Check(x, y);                                  
            }                 
        }

        private void Draw()
        {
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    MasLbl[i, j] = new Label();
                    MasLbl[i, j].Width = 60;
                    MasLbl[i, j].Height = 60;
                    MasLbl[i, j].Location = new Point(60 * j, 60 * i);
                    Controls.Add(MasLbl[i, j]);
                    MasLbl[i, j].BorderStyle = BorderStyle.FixedSingle;
                    MasLbl[i, j].Click += new EventHandler(Mas_Click);
                    MasLbl[i, j].Font = new Font("Arial", 20);
                    MasLbl[i, j].TextAlign = ContentAlignment.MiddleCenter;
                }
            }
            for (int i = 0; i < 3; i++)            
                for (int j = 0; j < 3; j++)                
                    MasLbl[i, j].BackColor = Color.Silver;

            for (int i = 0; i < 3; i++)
                for (int j = 6; j < 9; j++)
                    MasLbl[i, j].BackColor = Color.SteelBlue;

            for (int i = 6; i < 9; i++)
                for (int j = 6; j < 9; j++)
                    MasLbl[i, j].BackColor = Color.Tan;

            for (int i = 6; i < 9; i++)
                for (int j = 0; j < 3; j++)
                    MasLbl[i, j].BackColor = Color.LightBlue;

            for (int i = 3; i < 6; i++)
                for (int j = 3; j < 6; j++)
                    MasLbl[i, j].BackColor = Color.LightCoral;
        }
             
        private void Check(int x, int y)
        {
            if (number.ShowDialog() == DialogResult.OK)
            {
                if (!sudMas.SetValue(x, y, number.SelectedNumber()))
                {
                    for (int i = 0; i < 9; i++)
                        for (int j = 0; j < 9; j++)
                            if (sudMas.Duplicates[i, j])
                                MasLbl[i, j].ForeColor = Color.Red;
                    MessageBox.Show("Эта цифра уже существует!");
                    for (int i = 0; i < 9; i++)
                    {
                        for (int j = 0; j < 9; j++)
                        {
                            MasLbl[i, j].ForeColor = Color.Black;
                        }
                    }
                }
                else
                {                    
                    MasLbl[y, x].Text = number.SelectedNumber().ToString();
                    sudMas.Mas[y, x] = int.Parse(MasLbl[y, x].Text);
                    MasLbl[y, x].BackColor = Color.Yellow;
                    
                    amount++;
                    if (amount == 81)
                    {
                        if (Settings.SelectedLevel == Settings.MaxLevel)
                            Settings.MaxLevel++;
                        TransitionToANewLevel Transition = new TransitionToANewLevel();
                        this.Hide();
                        Transition.ShowDialog();
                        
                    }

                }
            }
         }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите выйти?", "выход", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                Application.Exit();
        }

        private void butnreturn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите прервать игру и вернуться в главное меню?", "выход", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                this.Hide();
                Menu men = new Menu();
                men.Show();
            }
            
        }

        
    }
}
